package com.example.messages.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {
    @Bean
    public ConnectionFactory connectionFactory () {
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setAddresses("106.3.97.187:5672");
        cachingConnectionFactory.setUsername("admin");
        cachingConnectionFactory.setPassword("admin");
        cachingConnectionFactory.setVirtualHost("/HealthBox");
        return cachingConnectionFactory;
    }

    @Bean
    public RabbitTemplate rabbitTemplate(@Autowired ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }

    @Bean
    public Exchange exchange() {
        //new FanoutExchange()
        //new TopicExchange()

        //创建Direct类型的交换器
        return new DirectExchange("amq.direct", true, false, null);
    }

    @Bean
    public Queue queue() {
        //创建队列
        return new Queue("MessageService");
    }

    @Bean
    public Binding binding() {
        //绑定交换器和队列
        return new Binding("MessageService", Binding.DestinationType.QUEUE, "amq.direct", "direct", null);
    }

}